import type { UserConfig, ConfigEnv } from 'vite';
import type { UserConfig as TestUserConfig } from 'vitest/config';
import path from 'path';
import vue from '@vitejs/plugin-vue';
import AutoImport from 'unplugin-auto-import/vite';
import themePreprocessorPlugin from '@zougt/vite-plugin-theme-preprocessor';
import ViteComponents from 'unplugin-vue-components/vite';
import { AntDesignVueResolver } from 'unplugin-vue-components/resolvers';
import progress from 'vite-plugin-progress';
import { ProjectSetting } from './src/setting/project-setting';
import { AppConfig } from './src/config/app-config';
import { injectHtml } from 'vite-plugin-html';

function pathResolve(dir: string) {
	return path.join(__dirname, dir);
}

// mode dev/production
function computedPlugins(mode) {
	const plugins = [
		vue(),
		// 主题相关
		(themePreprocessorPlugin as any).default({
			less: {
				// 各个主题文件的位置
				multipleScopeVars: [
					{
						scopeName: 'theme-default',
						path: path.resolve('src/styles/theme/default.less'),
					},
					{
						scopeName: 'theme-dark',
						path: path.resolve('src/styles/theme/dark.less'),
					},
				],
			},
		}),
		//需要定义变量直接在data里接着定义就好了
		injectHtml({
			data: {
				title: AppConfig.appTitle ? AppConfig.appTitle : AppConfig.appName,
				favicon: '/vite.svg',
				icon: '/vite.svg',
				loadTitle: AppConfig.appTitle ? AppConfig.appTitle : AppConfig.appName,
			},
		}),
		// 自动导入
		AutoImport({
			imports: ['vue'],
		}),
		// 打包进度
		progress(),
	];
	if (mode && mode === 'production') {
		plugins.push(
			ViteComponents({
				resolvers: [AntDesignVueResolver()],
			})
		);
	}
	return plugins;
}

// 获取基础路径
function getBaseUrl() {
	if (ProjectSetting.routeMode === 'HISTORY') {
		return `/${ProjectSetting.resourceBaseUrl}/`;
	} else {
		if (ProjectSetting.resourceBaseUrl) {
			if (ProjectSetting.resourceBaseUrl === './') {
				return './';
			} else {
				return `/${ProjectSetting.resourceBaseUrl}/`;
			}
		} else {
			return './';
		}
	}
}

// 获取配置颜色对象
function getConfigColorVar() {
	let colorVar = {};
	if (ProjectSetting.headerSetting.bgColor) {
		colorVar['@config-header-bg-light'] = ProjectSetting.headerSetting.bgColor;
		colorVar['@config-header-bg-dark'] = ProjectSetting.headerSetting.bgColor;
	}
	if (ProjectSetting.menuSetting.bgColor) {
		colorVar['@config-menu-bg-light'] = ProjectSetting.menuSetting.bgColor;
		colorVar['@config-menu-bg-dark'] = ProjectSetting.menuSetting.bgColor;
	}
	if (ProjectSetting.borderRadius) {
		colorVar['@config-border-radius'] = `${ProjectSetting.borderRadius}px`;
	}
	return colorVar;
}

export default ({ command, mode }: ConfigEnv): UserConfig & TestUserConfig => {
	let colorVar = getConfigColorVar();
	// 详细配置参见：https://cn.vitejs.dev/config/
	return {
		optimizeDeps: {
			include: [
				'@vue/shared',
				'ant-design-vue/es/locale/zh_CN',
				'ant-design-vue/es/locale/en_US',
			],
		},
		base: getBaseUrl(),
		build: {
			outDir: path.resolve(__dirname, 'dist'),
			// 规定触发警告的 chunk 大小
			chunkSizeWarningLimit: 1024
		},
		plugins: computedPlugins(mode),
		resolve: {
			alias: [
				{
					find: /@\//,
					replacement: pathResolve('src') + '/',
				},
				{
					find: /@components\//,
					replacement: pathResolve('src') + '/components/',
				},
				{
					find: /@views\//,
					replacement: pathResolve('src') + '/views/',
				},
				{
					find: /@widgets\//,
					replacement: pathResolve('src') + '/widgets/',
				},
				{
					find: /@plugins\//,
					replacement: pathResolve('src') + '/plugins/',
				},
			],
		},
		server: {
			port: 8080,
			proxy: {
				'/api': {
					target: 'http://172.16.152.66:30223/',
					// target: 'http://172.16.152.57:30123/',
					// target: 'http://172.16.240.110:51001',
					changeOrigin: true,
					rewrite: (path) => path.replace(/^\/api/, ''),
				},
			},
		},
		css: {
			preprocessorOptions: {
				less: {
					javascriptEnabled: true,
					modifyVars: colorVar,
				},
			},
		},
		// 详细配置参见：https://cn.vitest.dev/config/
		test: {
			globals: true,
			include: ['**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
			testTimeout: 200000,
			environment: 'jsdom',
			coverage: {
				reporter: ['text', 'json', 'html', 'lcov'],
				exclude: [
					'**/src/api/**',
					'**/src/components/**',
					'**/src/directives/**',
					'**/src/plugins/**',
					'**/src/ui-service/**',
					'**/src/views/**',
					'**/src/widgets/**',
					'**/tests/**',
				],
			},
			setupFiles: ['./tests/global/setup.ts'],
		},
	};
};
